package com.carsa.blackberry.ui.screen.gruposentregas;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;

import com.carsa.blackberry.CarsaApp;
import com.carsa.blackberry.MainKeys;
import com.carsa.blackberry.domain.dto.EntregaDTO;
import com.carsa.blackberry.domain.services.impl.EntregasServiceImpl;
import com.carsa.blackberry.ui.UIKeys;
import com.carsa.blackberry.ui.screen.MainMenuScreen;
import com.carsa.blackberry.ui.screen.common.BasicScreen;
import com.carsa.blackberry.ui.screen.common.DetalleGrupoEntregaScreen;
import com.carsa.blackberry.util.ui.UIUtil;
import com.carsa.blackberry.util.ui.widget.GridFieldManager;

/**
 * @author Nahuel Barrios.
 * @since 20/07/2010.
 * @version 1.0
 */
public class GruposEntregasScreen extends BasicScreen {

    /**
     * Sobreescribo la funcionalidad por default que puse en BasicScreen para cada pantalla que tenga el boton "volver".
     * Hago esto, en este metodo porque el boton "volver" lo que hace cuando es presionado es llamar a este metodo.
     */
    public boolean onClose() {

	UiApplication.getUiApplication().popScreen(this);
	UiApplication.getUiApplication().pushScreen(new MainMenuScreen());
	return false;
    }

    public GruposEntregasScreen(boolean hasToShowMessage) {
	super(UIKeys.TITLE_GRUPOS_ENTREGAS);
	super.customizeScreen();

	Vector data = new Vector();

	// Preparo los headers de la tabla.
	Vector headers = new Vector();
	headers.addElement(UIKeys.TITLE_GROUP);
	headers.addElement(UIKeys.TITLE_ENTREGA);
	headers.addElement(UIKeys.TITLE_CLIENT);
	headers.addElement("");
	data.addElement(UIUtil.prepareHeadersForGrid(headers, this));

	Vector entregas = new EntregasServiceImpl().getEntregasPendientes();
	String message;
	Boolean flag = (Boolean) CarsaApp.getMemory().remove(MainKeys.PARAMETER_FLAG);
	if (hasToShowMessage && flag != null) {
	    if (flag.booleanValue())
		message = UIKeys.POPUP_SE_RENOVARON_LAS_ENTREGAS_CORRECTAMENTE;
	    else
		message = UIKeys.POPUP_NO_HAY_ENTREGAS_NUEVAS;
	    Dialog.alert(message);
	}

	Enumeration elements = entregas.elements();
	while (elements.hasMoreElements()) {
	    EntregaDTO unaEntrega = (EntregaDTO) elements.nextElement();
	    data.addElement(this.prepareFieldArray(unaEntrega.getSammg(), unaEntrega.getVbeln(), unaEntrega.getName1()
		    + ", " + unaEntrega.getName2()));
	}
	LabelField lbl_withouthEntregas= null;
	if (entregas.size()==0) {
	    lbl_withouthEntregas = new LabelField(UIKeys.TITLE_NO_HAY_ENTREGAS_PARA_MOSTRAR); 
	}

	GridFieldManager aGrid = new GridFieldManager(new int[] { 78, 78, 124, 39 }, 0);// Nota: La sumatoria no da 320,
	// pero asi esta OK.
	aGrid = UIUtil.loadFieldGridFrom(aGrid, data);
	_mainManager.add(aGrid);
	
	if(lbl_withouthEntregas!=null){
	    _mainManager.add(UIUtil.getEmptyLine());
	    _mainManager.add(lbl_withouthEntregas);
	    _mainManager.add(UIUtil.getEmptyLineForResize(5));
	}

	_mainManager.add(UIUtil.getEmptyLine());
	super.addBackButton(_mainManager);
	this.add(_mainManager);
    }
    
    

    /**
     * Prepara el array de Field necesario para mostrar la tabla.
     * 
     * @param groupNumber
     * @param numeroEntrega
     * @param clientName
     * @return
     */
    public Field[] prepareFieldArray(final String groupNumber, final String numeroEntrega, final String clientName) {

	Field[] row = new Field[4];
	row[0] = new LabelField(groupNumber, FIELD_HCENTER | Field.NON_FOCUSABLE);
	row[1] = new LabelField(numeroEntrega, FIELD_HCENTER | Field.NON_FOCUSABLE);
	row[2] = new LabelField(clientName, FIELD_HCENTER | Field.NON_FOCUSABLE);

	ButtonField bf_view = new ButtonField(UIKeys.TITLE_VER);
	bf_view.setFont(this.getFont().getFontFamily().getFont(Font.PLAIN, UIKeys.SIZE_FOR_LITTLE_BUTTONS_FONT));
	bf_view.setChangeListener(new FieldChangeListener() {

	    public void fieldChanged(Field field, int context) {
		Hashtable parameters = new Hashtable();

		Vector entregas = new EntregasServiceImpl().getEntregasPendientes();

		Enumeration elements = entregas.elements();
		while (elements.hasMoreElements()) {
		    EntregaDTO unaEntrega = (EntregaDTO) elements.nextElement();

		    if (unaEntrega.getVbeln().equals(numeroEntrega))
			parameters.put(MainKeys.PARAMETER_CURRENT_ENTREGA, unaEntrega);
		}

//		UiApplication.getUiApplication().popScreen(_currentScreen);
		UiApplication.getUiApplication().pushScreen(new DetalleGrupoEntregaScreen(parameters));
	    }

	});
	row[3] = bf_view;

	return UIUtil.setFontSize(row, UIKeys.SIZE_FOR_GRID_DATA_FONT);
    }

}
